package com.pengshun.modules.api.user.controller;

import com.pengshun.modules.api.coal.service.dto.MerchantDto;
import com.pengshun.modules.domain.User;
import com.pengshun.modules.api.coal.validator.*;
import com.pengshun.modules.api.user.para.UserPara;
import com.pengshun.modules.api.user.service.UserService;
import com.pengshun.modules.security.Authorization;
import com.pengshun.modules.security.CurrUserInfo;
import com.pengshun.modules.domain.UserWechat;
import com.pengshun.modules.common.BaseResult;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @ClassName: UserController
 * @Description: 用户管理
 * @Author: lifei
 * @Date: 2020/11/4 10:24 上午
 * @Version: 1.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "用户管理")
@RequestMapping("/api/user")
public class UserController {

    private final UserService userService;

    @ApiOperation(value = "获取验证码", notes = "")
    @GetMapping(value = "/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "注册传(resgtion) 登陆传(login)忘记密码传(forget) 绑定手机号传biding,更换手机号传changePhone 更改支付密码传paypass 设置支付密码 setpayass", dataType = "string", required = true, paramType = "query"),
    })
    public BaseResult getCode(@ApiIgnore @Validated(value = {First.class}) UserPara para) throws Exception {
        BaseResult res = userService.getCode(para);
        return res;
    }

    @ApiOperation(value = "验证码验证", notes = "")
    @PostMapping(value = "/verify")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "注册传(resgtion) 登陆传(login)忘记密码传(forget) 绑定手机号传biding,更换手机号传changePhone 更改支付密码传paypass", dataType = "string", required = true, paramType = "query"),
    })
    public BaseResult verify(@ApiIgnore @Validated(value = {First.class}) UserPara para) throws Exception {
        BaseResult res = userService.verifyCode(para);
        return res;
    }


    @ApiOperation(value = "微信登陆", notes = "")
    @GetMapping(value = "/wechat")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "nickName", value = "昵称", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省份", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "city", value = "城市", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "country", value = "区县", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "headimgurl", value = "头像", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "privilege", value = " ", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "unionid", value = "unionid", dataType = "string", paramType = "query"),
    })
    public BaseResult wechatLogin(@ApiIgnore @Validated(value = {First.class}) UserWechat para) throws Exception {
        BaseResult res = userService.wechatLogin(para);
        return res;
    }

    @ApiOperation(value = "微信绑定手机", notes = "")
    @GetMapping(value = "/wechat-biding")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", required = true, paramType = "query"),
    })
    public BaseResult wechatBiding(@ApiIgnore @Validated(value = {Second.class}) UserPara para) throws Exception {
        BaseResult res = userService.wechatBiding(para);
        return res;
    }

    @ApiOperation(value = "注册",notes = "如果携带openid证明先微信登录 在注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pass", value = "密码", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "string",  paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "0:普通用户,1司机,2车主,3运输公司,4煤炭商,", dataType = "int", required = true, paramType = "query"),
    })
    @PostMapping(value = "/resgtion", produces = "application/json; charset=utf-8")
    public BaseResult resgtion(@ApiIgnore @Validated(value = {Fifth.class}) UserPara para) throws Exception {
        BaseResult res = userService.resgtion(para);
        return res;
    }

    @ApiOperation(value = "更换角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "userType", value = "0:普通用户,1司机,2车主,3运输公司,4煤炭商,", dataType = "int", required = true, paramType = "query"),
    })
    @PostMapping(value = "/change", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult change(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Seventh.class}) UserPara para) throws Exception {
        para.setId(user.getId());
        BaseResult res = userService.change(para);
        return res;
    }

    @ApiOperation(value = "个人中心查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userType", value = "0:普通用户,1司机,2车主,3运输公司,4煤炭商,", dataType = "int", required = true, paramType = "query"),
//    })
    @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = false, paramType = "header")
    @PostMapping(value = "/center", produces = "application/json; charset=utf-8")
    public BaseResult center() {
        BaseResult res = userService.getCenter();
        return res;
    }


    @ApiOperation(value = "验证码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", required = true, paramType = "query")
    })
    @PostMapping(value = "/verificationLogin", produces = "application/json; charset=utf-8")
    public BaseResult verificationLogin(@ApiIgnore @Validated(value = {Second.class}) UserPara para) throws Exception {
        BaseResult res = userService.verificationLogin(para);
        return res;
    }

    @ApiOperation(value = "手机号密码码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pass", value = "密码", dataType = "string", required = true, paramType = "query")
    })
    @PostMapping(value = "/pass", produces = "application/json; charset=utf-8")
    public BaseResult passLogin(@ApiIgnore @Validated(value = {Fourth.class}) UserPara para) throws Exception {
        BaseResult res = userService.passLogin(para);
        return res;
    }

    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "oldPass", value = "旧密码", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "newPass", value = "新密码", dataType = "string", required = true, paramType = "query")
    })
    @Authorization
    public BaseResult updatePass(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Third.class}) UserPara para) throws Exception {
        para.setId(user.getId());
        BaseResult res = userService.updatePass(para);
        return res;
    }

    @ApiOperation("更换手机号")
    @PostMapping(value = "/change-phone")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
    })
    @Authorization
    public BaseResult changePhone(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Second.class}) UserPara para) throws Exception {
        para.setId(user.getId());
        BaseResult res = userService.changePhone(para);
        return res;
    }

    @ApiOperation(value = "修改用户信息资料", notes = "只修改头像就传头像，设置支付密码传payPass  重新修改 oldPayPass  payPass 都需要")
    @PostMapping(value = "/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "avatarName", value = "头像", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "nickName", value = "昵称", dataType = "string", paramType = "query"),
    })
    @Authorization
    public BaseResult update(@ApiIgnore @CurrUserInfo User user, @ApiIgnore UserPara para) throws Exception {
        para.setId(user.getId());
        BaseResult res = userService.update(para);
        return res;
    }

    @ApiOperation("忘记密码")
    @PostMapping(value = "/forgetPass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newPass", value = "新密码", dataType = "string", required = true, paramType = "query")
    })
    public BaseResult forgetPass(@ApiIgnore @Validated(value = {Sixth.class}) UserPara para) throws Exception {
        BaseResult res = userService.forgetPass(para);
        return res;
    }

    @ApiOperation("个人信息")
    @GetMapping(value = "/detil")
    @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    @Authorization
    public BaseResult detil(@ApiIgnore @CurrUserInfo User user) throws Exception {
        BaseResult res = userService.detil(user.getId());
        return res;
    }

    @ApiOperation("手机号查询司机或者车主")
    @GetMapping(value = "/phone")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "1司机 2车主", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @Authorization
    public BaseResult phone(@ApiIgnore @CurrUserInfo User u, @ApiIgnore User user) throws Exception {
        user.setId(u.getId());
        BaseResult res = userService.findByPhone(user);
        return res;
    }

    @ApiOperation(value = "设置支付密码", notes = "只修改头像就传头像，设置支付密码传payPass  重新修改 oldPayPass  payPass 都需要")
    @PostMapping(value = "/set-pay-pass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "payPass", value = "支付密码", dataType = "string", paramType = "query"),
    })
    @Authorization
    public BaseResult setPayPass(@ApiIgnore @CurrUserInfo User user, @ApiIgnore UserPara para) throws Exception {
        para.setId(user.getId());
        BaseResult res = userService.setPayPass(para);
        return res;
    }


    @ApiOperation(value = "修改支付密码", notes = "只修改头像就传头像，设置支付密码传payPass  重新修改 oldPayPass  payPass 都需要")
    @PostMapping(value = "/update-pay-pass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "type", value = "1密码修改2验证码修改", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "oldPayPass", value = "旧支付密码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "payPass", value = "支付密码", dataType = "string", paramType = "query"),
    })
    @Authorization
    public BaseResult updatePayPass(@ApiIgnore @CurrUserInfo User user, @ApiIgnore UserPara para) throws Exception {
        para.setId(user.getId());
        BaseResult res = userService.updatePayPass(para);
        return res;
    }


    @ApiOperation("商户完善资料")
    @PostMapping(value = "/binuess")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商户id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "city", value = "市", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "district", value = "区", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "adcode", value = "区编码", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "doorImg", value = "门头照片", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lngX", value = "经度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "latY", value = "纬度", dataType = "double", required = true, paramType = "query")
    })
    public BaseResult binuess(@ApiIgnore @Validated(value = {First.class}) MerchantDto para) throws Exception {
        BaseResult res = userService.binuess(para);
        return res;
    }

    @ApiOperation(value = "首页轮播记录", notes = "")
    @GetMapping(value = "/roll")
    public BaseResult getRoll() {
        BaseResult res = userService.getRoll();
        return res;
    }
}
